<template>
  <div>
    <van-nav-bar
      title="账号注册"
      left-arrow
      @click-left="onClickLeft"
      class="bgclo"
    />

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="name"
        placeholder="请输入手机号"
        :rules="mobile"
      />
      <van-field
        v-model="password"
        type="password"
        name="pass"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码' }]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit" class="deep"
          >注册</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { zhuceApi } from '@/api'
export default {
  methods: {
    //跳转到登录界面
    onClickLeft() {
      this.$router.push({ path: '/home/my' })
    },
    //提交注册申请
    async onSubmit() {
      try {
        this.$toast.loading({
          message: '注册中...',
          forbidClick: true
        })
        const { username, password } = this
        const aa = await zhuceApi(username, password)
        console.log(aa)
        if (aa.data.status === 400) {
          this.$toast.success(aa.data.description)
        } else if (aa.data.status === 200) {
          this.$toast.success('注册成功')
          setTimeout(() => {
            this.$router.push({ name: 'login' })
          }, 1000)
        }
      } catch (error) {
        if (error.data?.status) this.$toast.fail('注册失败')
        console.log(error)
        throw error
      }
    }
  },
  data() {
    const mobile = [
      { required: true, message: '请填写手机号', trigger: 'onChange' }
      // {
      //   pattern:
      //     /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
      //   message: "请正确填写",
      // },
    ]
    return {
      username: '',
      password: '',
      mobile
    }
  }
}
</script>

<style scoped lang="less">
.bgclo {
  background-color: pink;
  /deep/ .van-nav-bar__title {
    color: white !important;
  }
  /deep/ .van-icon-arrow-left {
    color: white !important;
  }
}
.deep {
  background-color: pink;
  border: 0.02667rem solid pink;
}
.p {
  margin-top: 45px;
  text-align: center;
  font-size: 12px;
}
</style>
